<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 5 Transitional//EN">
<html>
    <head>
        <title>根据地址查询经纬度</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
        body{background: #ebebeb;width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
        #map{height:450px;width:65%;float:center;margin: 0px auto;}
        #mai{text-align:center;}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=74af171e2b27ee021ed33e549a9d3fb9"></script>
    </head>
    
    <body>
    <br>
       <div id=mai>   
                查询的地址：<input id="text_" type="text" value="武汉大学" style="margin-right:100px;"/>
              <input type="button" value="查询" onclick="searchByStationName();"/>
       </div>
       <br>
       <div id="map"></div>
    </body>
</html>

<script type="text/javascript">
        var map = new BMap.Map("map");
        var point = new BMap.Point(114.31667,30.51667);
        map.centerAndZoom(point, 14);
        map.enableScrollWheelZoom();    //启用滚轮放大缩小，默认禁用
        map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
        map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
        map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
        var localSearch = new BMap.LocalSearch(map);
        localSearch.enableAutoViewport(); //允许自动调节窗体大小
        
    function searchByStationName() {
        map.clearOverlays();//清空原来的标注
        var keyword = document.getElementById("text_").value;
        localSearch.search(keyword);
        
        localSearch.setSearchCompleteCallback(function (searchResult) {
        var poi = searchResult.getPoi(0);   //搜索到的位置结果
        map.centerAndZoom(poi.point, 14);
        var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注，为要查询的地方对应的经纬度
        map.addOverlay(marker);
        
        var content = document.getElementById("text_").value + "<br/><br/>经度：" + poi.point.lng + "<br/>纬度：" + poi.point.lat;
        var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + content + "</p>");
        marker.addEventListener("click", function () { this.openInfoWindow(infoWindow); });
        });
    } 
</script>